package com.ithengxing.d2_udp1;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ServerUdp {
    public static void main(String[] args) throws Exception {
        System.out.println("===========服务端启动=============");
        //1.创建接收对象（人）注册端口
        DatagramSocket sc = new DatagramSocket(8888);

        //创建一个数据包对象接收客户端传输过来的数据
        byte[] bytes = new byte[1024 * 60];
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);

        //等待接收数据
        sc.receive(packet);

        //取出数据
        int len = packet.getLength();
        String rs = new String(bytes, 0, len);
        System.out.println("收到了：" + rs);

        //获取发送端的ip
        String ip = packet.getSocketAddress().toString();
        System.out.println("接收到发送端的ip：" + ip);

        //获取发送端的端口
        int port = packet.getPort();
        System.out.println("获取到发送端的端口：" + port);

        //关闭接收
        sc.close();

    }
}
